/*
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.dsteiner.widgets.client.text;

import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.TextInput;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.RichTextArea.BasicFormatter;
import com.google.gwt.user.client.ui.RichTextArea.ExtendedFormatter;

/**
 * Example file.
 */
public class RichTextAreaExt extends LazyWidget implements TextInput{
	
	private RichTextArea area = new RichTextArea();

	@Override
	public Widget createContentWidget() {
	    // Create the text area and toolbar
	    RichTextToolbar toolbar = new RichTextToolbar(area);
	    toolbar.setWidth("100%");

	    // Add the components to a panel
	    Grid grid = new Grid(2, 1);
	    grid.setStyleName("richText");
	    grid.setWidget(0, 0, toolbar);
	    grid.setWidget(1, 0, area);
	    area.setWidth("100%");
	    return grid;
	}

	public void selectAll() {
		BasicFormatter basicFormatter = area.getBasicFormatter();
		if(basicFormatter != null)
			basicFormatter.selectAll();
		ExtendedFormatter extendedFormatter = area.getExtendedFormatter();
		if(extendedFormatter != null)
			extendedFormatter.selectAll();
	}

	public void setCursorPos(int pos) {
	}

	public void setEnabled(boolean enabled) {
		area.setEnabled(enabled);
	}

	public String getText() {
		return area.getHTML();
	}

	public void setText(String text) {
		area.setHTML(text);
	}

	public String getName() {
		return area.getElement().getAttribute("name");
	}

	public void setName(String name) {
		area.getElement().setAttribute("name", name);
	}

	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return area.addClickHandler(handler);
	}

	public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
		return area.addKeyDownHandler(handler);
	}

	public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
		return area.addKeyPressHandler(handler);
	}

	public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
		return area.addKeyUpHandler(handler);
	}

	public HandlerRegistration addFocusHandler(FocusHandler handler) {
		return area.addFocusHandler(handler);
	}

	public HandlerRegistration addBlurHandler(BlurHandler handler) {
		return area.addBlurHandler(handler);
	}

	public int getTabIndex() {
		return area.getTabIndex();
	}

	public void setAccessKey(char key) {
		area.setAccessKey(key);
	}

	public void setFocus(boolean focused) {
		area.setFocus(focused);
	}

	public void setTabIndex(int index) {
		area.setTabIndex(index);
	}

	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		throw new UnsupportedOperationException();
	}
}
